package cn.tedu;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.Timer;
import javax.swing.*;
import java.awt.*;
import java.util.TimerTask;

public class MyPanel extends JPanel implements MouseMotionListener, MouseListener {
    Random random=new Random();
//    Star star = new Star();

    int aliveBall=16;

    Ball[] balls = new Ball[aliveBall];
    Attacker attacker =new Attacker();
    Bullet bullet =new Bullet();
    long Score=0;
    int life=9999;



    public MyPanel(){
        this.setBackground(Color.GRAY);
        for(int i = 0; i< balls.length; i++){
            balls[i]=new Ball();
        }
        addMouseMotionListener(this);
        addMouseListener(this);
    }


    public void paint(Graphics g){

        super.paint(g);
        Font font = new Font("宋体",Font.BOLD,50);
        this.setFont(font);
        g.setColor(Color.YELLOW);
        g.drawString("点击左键射击    当前分数："+Score,50,50);



        for(int i = 0; i< balls.length; i++){
            g.setColor(balls[i].color);
            if(balls[i].alive==1){
                g.fillOval(balls[i].x,balls[i].y,balls[i].width,balls[i].height);
            }
        }
        g.setColor(Color.BLACK);
        g.fillRect(attacker.x,attacker.y,attacker.width,attacker.height);
        g.fillOval(bullet.x,bullet.y,bullet.width,bullet.height);
    }

    public void action(){
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                for (int i = 0; i< balls.length; i++) {
                    balls[i].move();
                    if(bullet.flag==1){
                        bullet.move();
                    }
                    if(balls[i].y<=bullet.y && balls[i].x<bullet.x && (balls[i].x+balls[i].width)>bullet.x){
                        balls[i].color=new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
                        balls[i].alive=0;
                        //不能在这里判断存活，否则会被重复判定
                        Score+=1;
                    }
                }
                int remake=16;
                for(int i=0;i<balls.length;i++){
                    if(balls[i].alive==0){
                        remake-=1;
                    }
                }
                if(remake<2){
                    for(int i=0;i<balls.length;i++){
                        balls[i].alive=1;
                    }
                }
                repaint();
            }
        },0,5);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        attacker.x=e.getX()-attacker.width/2;
        attacker.y=e.getY()-attacker.height/2;
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        attacker.x=e.getX()-attacker.width/2;
        attacker.y=e.getY()-attacker.height/2;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        bullet.x=e.getX();
        bullet.y=e.getY();
        bullet.flag=1;
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
